package com.demo.utils;

import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.HeaderParameter;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springdoc.core.GroupedOpenApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import java.net.InetAddress;

/***
 * 创建Swagger配置
 * &#064;since:knife4j-springdoc-openapi-demo  1.0
 * @author <a href="mailto:xiaoymin@foxmail.com">xiaoymin@foxmail.com</a>
 * 2020/03/15 20:40
 */
@Configuration
@Slf4j
public class SwaggerConfig implements ApplicationRunner {

    @Autowired
    private Environment environment;


//    @Bean
//    public OpenAPI customOpenAPI() {
//        Components components = new Components();
//        //添加右上角的统一安全认证
//        components.addSecuritySchemes(SaManager.getConfig().getTokenName(),
//                new SecurityScheme()
//                        .type(SecurityScheme.Type.APIKEY)
//                        .scheme("basic")
//                        .name(SaManager.getConfig().getTokenName())
//                        .name(RequestHeaderConstants.BRAND_NAME)
//                        .name(RequestHeaderConstants.CHANNEL_NAME)
//                        .in(SecurityScheme.In.HEADER)
//                        .description("请求头")
//        );
//        return new OpenAPI()
//                .components(components)
//                .info(apiInfo());
//    }

    private Info apiInfo() {
        return new Info()
                .title("管理系统")
                .version("1.0")

                .license(new License().name("Apache 2.0")
                );
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 打印swagger url,取当项目的访问地址
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = this.environment.getProperty("server.port");
        if (StringUtils.isEmpty(port)) {
            port = "8080";
        }

        String path = this.environment.getProperty("server.servlet.context-path");
        if (StringUtils.isBlank(path) || "/".equals(path)) {
            path = "";
        }

        log.info("-------------------------------------------------------------------------");
        log.info("服务启动成功,SpringDocUrl: http://" + ip + ":" + port + path + "/doc.html");
        log.info("-------------------------------------------------------------------------");
    }
}
